namespace Crucible.Migrations
{
    using Models;
    using System;
    using System.Configuration;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Web.Security;
    using WebMatrix.WebData;

    internal sealed class Configuration : DbMigrationsConfiguration<ArgumentConnection>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ArgumentConnection context)
        {

            WebSecurity.InitializeDatabaseConnection("ArgumentConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

            if (!Roles.RoleExists("admin"))
            {
                Roles.CreateRole("admin");
            }

            if (!WebSecurity.UserExists("tarehart"))
            {
                WebSecurity.CreateUserAndAccount("tarehart", "yellow");
            }

            if (!Roles.GetRolesForUser("tarehart").Contains("admin"))
            {
                Roles.AddUsersToRoles(new[] { "tarehart" }, new[] { "admin" });
            }

            if (ConfigurationManager.AppSettings["seedDebugData"] == "true")
            {
                SeedDebugData(context);
            }   

        }

        private static void SeedDebugData(ArgumentConnection context)
        {
            UserProfile tyler = context.UserProfiles.First(p => p.UserName == "tarehart");

            if (!context.Nodes.Any())
            {

                context.Nodes.AddOrUpdate(
                    s => s.Id,
                    new Node() { Id = 2, DateOriginated = new DateTime(2007, 1, 1), AddedBy_UserId = tyler.UserId, Title = "IPCC Fourth Assessment Report, Summary for Policymakers", Link = "http://www.ipcc.ch/pdf/assessment-report/ar4/wg1/ar4-wg1-spm.pdf" },
                    new Node() { Type = NodeType.Originator, Id = 1, Title = "Intergovernmental Panel on Climate Change", Link = "http://www.ipcc.ch", AddedBy_UserId = tyler.UserId}
                );

                context.NodeLinks.AddOrUpdate(
                    k => k.Id,
                    new NodeLink() { Id = 1, Parent_Id = 2, Child_Id = 1 }
                );

            }

            context.SaveChanges();

            //if (tyler != null)
            //{

            //    if (!context.Nodes.Any())
            //    {
            //        context.Nodes.AddOrUpdate(
            //            a => a.Id,
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 1, Title = "Charleston is a nice place", Body = "Charleston is nice because {{[2]the weather is mild}} and {{[3]the internet is good}}. {{[5]A good internet connection is important}}." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 2, Title = "Charleston weather is mild", Body = "{{[4]Mild weather is sunny or cloudy near 70 degrees}}. Charleston is that way for 60% of the year." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 3, Title = "The internet service in Charleston is good", Body = "My download bandwidth is abc, and my upload is xyz." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 4, Title = "Mild weather is sunny or cloudy near 70 degrees", Body = "Here's a dictionary definition, and something from an encyclopedia." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 5, Title = "It's important to have a good internet connection", Body = "High quality internet connections support both {{[6]productivity}} and {{[7]recreation}}, leading to happiness." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 6, Title = "Fast internet makes people productive", Body = "Here's a study." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 7, Title = "You can have lots of fun with a fast internet connection", Body = "Games are super fun, and you're better at them with a fast connection. Anecdotally, {{[8]I rock at videogames}}, and my {{[3]Charleston internet is good}}." },
            //            new Persuasive() { Type = NodeType.Assertion, AddedBy = tyler, Id = 8, Title = "Tyler Arehart rocks at videogames", Body = "Here are my amazing stats." }
            //        );

            //        context.SaveChanges();

            //        context.NodeLinks.AddOrUpdate(a => a.Id,
            //            new NodeLink() { Id = 1, Parent_Id = 1, Child_Id = 2 },
            //            new NodeLink() { Id = 2, Parent_Id = 1, Child_Id = 3 },
            //            new NodeLink() { Id = 3, Parent_Id = 1, Child_Id = 5 },
            //            new NodeLink() { Id = 4, Parent_Id = 2, Child_Id = 4 },
            //            new NodeLink() { Id = 5, Parent_Id = 5, Child_Id = 6 },
            //            new NodeLink() { Id = 6, Parent_Id = 5, Child_Id = 7 },
            //            new NodeLink() { Id = 7, Parent_Id = 7, Child_Id = 8 },
            //            new NodeLink() { Id = 8, Parent_Id = 7, Child_Id = 3 }
            //        );

            //    }


            
            //    context.Comments.AddOrUpdate(
            //        c => c.Id,
            //        new Comment() { Id = 1, Author_UserId = tyler.UserId, Assertion_Id = 1, Body = "First!", DateAdded = new DateTime(2012, 12, 19)},
            //        new Comment() { Id = 2, Author_UserId = tyler.UserId, Assertion_Id = 1, Body = "Here's a more worthy comment", DateAdded = new DateTime(2012, 12, 20) }
            //        );

            //    context.Comments.AddOrUpdate(
            //        c => c.Id,
            //        new Comment() { Id = 3, Author_UserId = tyler.UserId, Assertion_Id = 1, Body = "Reply 1", Parent_Id = 2, DateAdded = new DateTime(2012, 12, 21) },
            //        new Comment() { Id = 4, Author_UserId = tyler.UserId, Assertion_Id = 1, Body = "Reply 2", Parent_Id = 2, DateAdded = new DateTime(2012, 12, 22) }
            //        );
            //}

            context.Fallacies.AddOrUpdate(
                f => f.Id,
                new Fallacy() { Id = 1, Name = "Appeal to probability", Description = "Assumes that because something could happen, it is inevitable that it will happen", Link = "http://en.wikipedia.org/wiki/Appeal_to_probability" },
                new Fallacy() { Id = 2, Name = "Argument from fallacy", Description = "Assumes that if an argument for some conclusion is fallacious, then the conclusion itself is false", Link = "http://en.wikipedia.org/wiki/Argument_from_fallacy" },
                new Fallacy() { Id = 3, Name = "Base rate fallacy", Description = "Makes a probability judgement based on conditional probabilities, without taking into account the effect of prior probabilities", Link = "http://en.wikipedia.org/wiki/Base_rate_fallacy" },
                new Fallacy() { Id = 4, Name = "Conjunction fallacy", Description = "Assumes that an outcome simultaneously satisfying multiple conditions is more probable than an outcome satisfying a single one of them", Link = "http://en.wikipedia.org/wiki/Conjunction_fallacy" },
                new Fallacy() { Id = 5, Name = "Masked man fallacy", Description = "The substitution of identical designators in a true statement leads to a false one", Link = "http://en.wikipedia.org/wiki/Masked_man_fallacy" },

                new Fallacy() { Id = 6, Name = "Begging the question", Description = "Assumes the conclusion of an argument in one of the premises", Link = "http://en.wikipedia.org/wiki/Begging_the_question" },
                new Fallacy() { Id = 7, Name = "Tu Quoque", Description = "Asserting that a position should be disregarded because it is hypocritical", Link = "http://en.wikipedia.org/wiki/Tu_quoque" }
            );

            context.SaveChanges();
        }
    }
}
